﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml;

namespace XTKF
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // 应用程序启动时
            //System.Timers.Timer timer = new System.Timers.Timer();
            //timer.Interval = 1000 * 1;
            //timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
            //timer.Enabled = true;
        }

        void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DateTime startTime = Convert.ToDateTime(Get_XmlSet("StartTime"));
            DateTime currentTime = DateTime.Now;
            TimeSpan tSpan = currentTime - startTime;
            if (tSpan.Days.ToString() == Get_XmlSet("IntervalDay"))
            {
                #region 数据库操作
//                string sql_min = @"select min(nID) as mixID from  NewsTemp where isfb=0";
//                string minID = new Operation().ExScalar(CommandType.Text, sql_min).ToString();

//                string sql_insert_news = @"insert into News(nTitle,nContent,nLaiyuan,nDate,bs,nJJ)
//                                                           select nTitle,nContent,nLaiyuan,nDate,bs,nJJ from  NewsTemp where nID='" + minID + "'";
//                if (new Operation().ExNoQuery(CommandType.Text, sql_insert_news) > 0)
//                {
//                    string sql_update_news_temp = @"update NewsTemp set isfb=1 where nID='" + minID + "'";
//                    new Operation().ExNoQuery(CommandType.Text, sql_update_news_temp);
//                    Update_XmlSet(currentTime.ToString("yyyy-MM-dd HH:mm:ss"));
//                    Create.splitpage(1);
//                }
                #endregion
            }
        }
        /// <summary>
        /// 根据key获取XML文件属性值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        string Get_XmlSet(string key)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, "Set.xml"));
            XmlNode timeNode = xmlDoc.SelectSingleNode("add");
            string time = string.Empty;
            XmlElement e = ((XmlElement)timeNode);
            time = e.GetAttribute(key).ToString().Trim();
            return time;
        }
        public void Update_XmlSet(string value)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, "Set.xml"));
            XmlNode timeNode = xmlDoc.SelectSingleNode("add");
            XmlElement e = ((XmlElement)timeNode);
            e.SetAttribute("StartTime", value);
            xmlDoc.Save(System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, "Set.xml"));

        }
        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码

        }

        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码

        }

        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码

        }

        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时，才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer，则不会引发该事件。

        }

    }
}
